/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/30
 */

import Vue from 'vue';
import commonService from '../common/commonService';
import lib from '../common/lib';
import Loading from '../../grsc-base/component/Loading.vue';
import Validator from '../../grsc-base/component/Validator/main';
import {REG_EMAIL} from '../../grsc-base/common/constants';

export default Vue.extend({
    components: {
        Loading,
        Validator
    },
    data() {
        return {
            form: {
                email: '',
                password: '',
                code: ''
            },
            rules: {
                email: {
                    tip: '',
                    required: true,
                    requiredTip: this.$t('common.emailRequireTip'),
                    pattern: REG_EMAIL,
                    patternTip: window.CODE.EMAIL[this.$i18n.locale]
                },
                password: {
                    required: true,
                    requiredTip: this.$t('common.loginPassRequireTip'),
                },
                code: {

                }
            },

            needImgCode: false,
            imgCodeSrc: '',

            isRequesting: false,

            randomKey: ''   // 发送图形验证码需要的动态时间参数
        }
    },
    created() {

    },
    mounted(){
        let vm = this;
        setTimeout(function () {
            vm.init();
        },0)
    },
    watch: {

    },
    methods: {

        init(){
            this.form.email = '';
            this.form.password = '';
        },

        /**
         * 手机号失去焦点
         */
        blurEmail() {
            this.rules.email.$check();
            if (this.rules.email.$valid === true) {
                commonService.needCaptha({
                    userName: this.form.email
                }, {}).done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        this._hideCode();
                    } else if (rep.code === window.CODE.HAS_NO_REGISTER.CODE) {
                        this.rules.email.$setError(window.CODE.HAS_NO_REGISTER[this.$i18n.locale]);
                    } else if(rep.code === window.CODE.NO_CAPTCHA.CODE){
                        if(rep.data && rep.data.num && rep.data.num < 10){
                            this._showCode();
                        }else if(rep.data && rep.data.num && rep.data.num >= 10){
                            this.rules.email.$setError(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                        }
                    }else {
                        window.error(rep.msg);
                    }
                });
            }
        },

        /**
         * 改变图形验证码
         */
        changeImgCode() {
            this.randomKey = new Date().getTime();
            commonService.getCode({
                randomKey: this.randomKey
            }).done(rep => {
                this.imgCodeSrc = rep;
            });
        },

        _hideCode() {
            this.needImgCode = false;
            if (this.rules.code) {
                setTimeout(() => {
                    delete this.rules.code;
                    this.rules.$reload();
                }, 0);
            }
        },

        _showCode() {
            this.needImgCode = true;
            this.changeImgCode();
            setTimeout(() => {
                this.rules.code = {
                    required: true,
                    requiredTip: this.$t('common.captchaRequireTip')
                };
                this.rules.$reload();
            }, 0);
        },

        /**
         * 更新资产
         * */
        updateAccount(){

        },


        submit() {
            if (this.rules.$checkAll() === false) {
                return;
            }
            let header;

            // 不需要图形验证码
            if (this.needImgCode === false) {
                header = {
                    'account-no': this.form.email,
                    'login-password': this.form.password
                };
                this.isRequesting = true;
                commonService.login({}, header).done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        lib.doLogin(rep.data.token, rep.data.userAccount);
                        if (rep.data.authLevel === 'LEVEL1') {
                            lib.setAuthLevel1(user.account);
                        } else if(rep.data.authLevel === 'LEVEL2'){
                            lib.setAuthLevel2(user.account);
                        } else {
                            lib.removeAuth(user.account);
                        }
                    } else if (rep.code === window.CODE.HAS_NO_REGISTER.CODE) {
                        this.rules.email.$setError(window.CODE.HAS_NO_REGISTER[this.$i18n.locale]);
                    } else if (rep.code === window.CODE.NO_CAPTCHA.CODE) {
                        this._showCode();
                    } else if (rep.code === window.CODE.ACCOUNT_LOCK.CODE) {
                        this.rules.password.$setError(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                    } else {
                        this.rules.password.$setError(window.CODE.LOGIN_PWD[this.$i18n.locale]);
                        if (rep.code === window.CODE.LOGIN_PWD.CODE && rep.data && rep.data.num === 3) {
                            this._showCode();
                            return;
                        }
                        if (rep.data && +rep.data.needVerCode === 1) {
                            this._showCode();
                        }
                    }
                }).always(rep => {
                    this.isRequesting = false;
                });
            } else {    // 需要图形验证码
                // 校验验证码

                header = {
                    'account-no': this.form.email,
                    'login-password': this.form.password,
                    'captchar-code': this.form.code,
                    'captchar-no': this.randomKey
                };

                this.isRequesting = true;
                commonService.login({}, header).done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        lib.doLogin(rep.data.token, rep.data.userAccount);
                    } else {
                        this.isRequesting = false;
                        setTimeout(() => {
                            if (rep.code === window.CODE.LOGIN_PWD.CODE && rep.data && rep.data.num && rep.data.num <= 10) {
                                this.changeImgCode();
                                let num = +rep.data.num;
                                if (num >= 5) {
                                    if (num === 10) {
                                        this.rules.password.$setError(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                                    } else {
                                        this.rules.password.$setError(this.$t('common.IncorrectTip', {num: 10 - num}));
                                    }
                                } else if (num < 5) {
                                    this.rules.password.$setError(window.CODE.LOGIN_PWD[this.$i18n.locale]);
                                }
                            } else if (rep.code === window.CODE.HAS_NO_REGISTER.CODE) {
                                this.rules.email.$setError(window.CODE.HAS_NO_REGISTER[this.$i18n.locale]);
                            } else if (rep.code === window.CODE.LOGIN_PWD.CODE) {
                                this.changeImgCode();
                                this.rules.password.$setError(window.CODE.LOGIN_PWD[this.$i18n.locale]);
                            } else if (rep.code === window.CODE.LOGIN_PWD_FORMAT.CODE) {
                                this.rules.password.$setError(window.CODE.LOGIN_PWD_FORMAT[this.$i18n.locale]);
                            } else if (rep.code === window.CODE.CAPTCHA.CODE) {
                                this.changeImgCode();
                                this.rules.code.$setError(window.CODE.CAPTCHA[this.$i18n.locale]);
                            } else if (rep.code === window.CODE.CAPTCHA_INVALID.CODE) {
                                this.rules.code.$setError(window.CODE.CAPTCHA_INVALID[this.$i18n.locale]);
                            } else if (rep.code === window.CODE.ACCOUNT_LOCK.CODE) {
                                this.rules.password.$setError(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                            } else {
                                window.error(rep.msg);
                            }
                        }, 0);
                    }
                }).always(rep => {

                });
            }

        }

    }
});